﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
///SaveData 的摘要说明
/// </summary>
public class SaveData
{

    public SaveData()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }

    public static void NewPost(Reply rep)//发新回复
    {
        databaseDataContext ddc = new databaseDataContext();
        posts p = new posts();
        p.tid = rep.tid;
        p.uid = rep.auther.uid;
        p.username = rep.auther.username;
        p.title = rep.title;
        p.text = rep.text;
        p.posttime = rep.posttime;
        p.post_ip = rep.post_ip;
        p.edittime = rep.edittime;
        p.edit_uid = rep.edituid;
        p.status = rep.status;
        ddc.posts.InsertOnSubmit(p);
        ddc.SubmitChanges();
    }

    public static void Regist(members m)//新用户注册
    {
        databaseDataContext ddc = new databaseDataContext();
        ddc.members.InsertOnSubmit(m);
        ddc.SubmitChanges();
    }

    public static void UpdateTopic(int tid, Topic t)//更新一个主题，未完成
    {
        databaseDataContext ddc = new databaseDataContext();
        var query = from o in ddc.threads
                    where (o.tid == tid)
                    select o;
        threads s = (query.ToList())[0];
        s.title = t.title;
        s.edittime = t.edittime;
        s.edit_uid = t.edituid;
        //
    }

    public static void IncTopicViewNum(int tid)//主题浏览数+1
    {
        databaseDataContext ddc = new databaseDataContext();
        var query = from o in ddc.threads
                    where (o.tid == tid)
                    select o;
        threads s = (query.ToList())[0];
        s.viewnum = s.viewnum + 1;
        ddc.SubmitChanges();
    }

    public static void IncTopicRepNum(int tid, DateTime t)//主题回复数+1
    {
        databaseDataContext ddc = new databaseDataContext();
        var query = from o in ddc.threads
                    where (o.tid == tid)
                    select o;
        threads s = (query.ToList())[0];
        s.replynum = s.replynum + 1;
        s.lastreplytime = t;
        ddc.SubmitChanges();
    }

    public static void UpdateLastRep(int tid,DateTime t)//更新帖子最后回复
    {
        databaseDataContext ddc = new databaseDataContext();
        var query = from o in ddc.threads
                    where (o.tid == tid)
                    select o;
        threads s = (query.ToList())[0];
        s.lastreplytime = t;
        ddc.SubmitChanges();
    }

    public static int NewTopic(Topic top)//新主题
    {
        databaseDataContext ddc = new databaseDataContext();
        threads t = new threads();
        t.fid = top.fid;
        t.uid = top.uid;
        t.title = top.title;
        t.text = top.text;
        t.posttime = top.posttime;
        t.post_ip = top.postip;
        t.edittime = top.edittime;
        t.edit_uid = top.edituid;
        t.lastreplytime = top.posttime;
        t.ishighlight = top.ishighlight;
        t.istop = top.istop;
        t.isjinghua = top.isjinghua;
        t.viewnum = top.viewnum;
        t.replynum = top.replynum;
        t.status = top.status;
        t.ProbID = top.ProbID;
        ddc.threads.InsertOnSubmit(t);
        ddc.SubmitChanges();
        return t.tid;
    }

    public static int NewTopic(Topic top,int probID)//新主题
    {
        databaseDataContext ddc = new databaseDataContext();
        threads t = new threads();
        t.fid = top.fid;
        t.uid = top.uid;
        t.title = top.title;
        t.text = top.text;
        t.posttime = top.posttime;
        t.post_ip = top.postip;
        t.edittime = top.edittime;
        t.edit_uid = top.edituid;
        t.lastreplytime = top.posttime;
        t.ishighlight = top.ishighlight;
        t.istop = top.istop;
        t.isjinghua = top.isjinghua;
        t.viewnum = top.viewnum;
        t.replynum = top.replynum;
        t.status = top.status;
        t.ProbID = probID;
        ddc.threads.InsertOnSubmit(t);
        ddc.SubmitChanges();
        return t.tid;
    }

    public static void UpdateMember(MemDisplay m)//更新用户，未完成
    {
        databaseDataContext ddc = new databaseDataContext();
        var query=from o in ddc.members
                  where(o.uid==m.uid)
                  select o;
        members n=(query.ToList())[0];
        n.hasavatar = m.hasAvatar;
        n.nickname = m.nickname;
        n.brithday = m.birth;
        if (m.gender == "男")
            n.gender = 1;
        else if (m.gender == "女")
            n.gender = 0;
        else
            n.gender = null;
        n.QQ=m.QQ;
        n.email = m.email;
        n.department = m.department;
        if(m.password!=null)
            n.password = m.password;
        n.gid = m.gid;
        ddc.SubmitChanges();
    }

    public static void IncMemViewNum(int uid)//用户浏览数+1
    {
        databaseDataContext ddc = new databaseDataContext();
        var query = from o in ddc.members
                    where (o.uid == uid)
                    select o;
        members s = (query.ToList())[0];
        s.viewnum = s.viewnum + 1;
        ddc.SubmitChanges();
    }

    public static void IncMemRepNum(int uid,DateTime dt)//用户回复数+1并更新时间
    {
        databaseDataContext ddc = new databaseDataContext();
        var query = from o in ddc.members
                    where (o.uid == uid)
                    select o;
        members s = (query.ToList())[0];
        s.replynum = s.replynum + 1;
        s.lastpost_time = dt;
        ddc.SubmitChanges();
    }

    public static void IncMemTopNum(int uid, DateTime dt)//用户主题+1回复+1并更新时间
    {
        databaseDataContext ddc = new databaseDataContext();
        var query = from o in ddc.members
                    where (o.uid == uid)
                    select o;
        members s = (query.ToList())[0];
        s.replynum = s.replynum + 1;
        s.threadsnum = s.threadsnum + 1;
        s.lastpost_time = dt;
        ddc.SubmitChanges();
    }

    public static void IncMemOnlineTime(int uid,int minutes)
    {
        databaseDataContext ddc = new databaseDataContext();
        var query = from o in ddc.members
                    where (o.uid == uid)
                    select o;
        members s = (query.ToList())[0];
        s.onlinetime += minutes;
        ddc.SubmitChanges();
    }

    public static void DelThread(int tid)//删除帖子
    {
        databaseDataContext ddc = new databaseDataContext();
        var query = from o in ddc.threads
                    where (o.tid == tid)
                    select o;
        if (query.Count() != 0)
        {
            threads t = (query.ToList())[0];
            t.status = 0;
            t.fid = 5;
            ddc.SubmitChanges();
        }
    }

    public static void DelPost(int pid)//删除回复
    {
        databaseDataContext ddc = new databaseDataContext();
        var query = from o in ddc.posts
                    where (o.pid == pid)
                    select o;
        if (query.Count() != 0)
        {
            posts p = (query.ToList())[0];
            p.status = 0;
            ddc.SubmitChanges();
        }
    }

    public static int NewForum(CForum cf)//新版块
    {
        databaseDataContext ddc = new databaseDataContext();
        forums f = new forums();
        f.name = cf.m_name;
        f.description = cf.m_description;
        f.visible = cf.isVisible;
        ddc.forums.InsertOnSubmit(f);
        ddc.SubmitChanges();
        return f.fid;
    }

    public static void UpdateForum(CForum cf)//更新版块信息
    {
        databaseDataContext ddc = new databaseDataContext();
        forums f = ((from o in ddc.forums
                   where(o.fid==cf.m_fid)
                   select o).ToList())[0];
        f.name = cf.m_name;
        f.description = cf.m_description;
        ddc.SubmitChanges();
    }

    public static void ToggleForum(int fid)//删除一个版块
    {
        databaseDataContext ddc = new databaseDataContext();
        var query = from o in ddc.forums
                    where (o.fid == fid)
                    select o;
        if (query.Count() != 0)
        {
            forums p = (query.ToList())[0];
            if (p.visible == 1)
                p.visible = 0;
            else
                p.visible = 1;
            ddc.SubmitChanges();
        }
    }

    public static void DelMember(int uid)//删除一个用户
    {
        databaseDataContext ddc = new databaseDataContext();
        var query = from o in ddc.members
                    where (o.uid == uid)
                    select o;
        if(query.Count()!=0)
        {
            members m = (query.ToList())[0];
            m.gid = 4;
            ddc.SubmitChanges();
        }
        
    }

    public static void UpdateUserLogin(members m,string ip)
    {
        databaseDataContext ddc = new databaseDataContext();
        var query = from o in ddc.members
                    where (o.uid == m.uid)
                    select o;
        if (query.Count()!= 0)
        {
            members n = (query.ToList())[0];
            n.lastlogin_ip = ip;
            n.lastlogin_time = DateTime.Now;
            ddc.SubmitChanges();
        }
    }

    public static void UpdateGroup(Permission p)//更新一个用户组
    {
        databaseDataContext ddc = new databaseDataContext();
        var query = from o in ddc.groups
                    where (o.gid == p.gid)
                    select o;
        if(query.Count()!=0)
        {
            groups g = (query.ToList())[0];
            g.name = p.name;
            g.allowaccess = p.awaccess;
            g.allowdelete = p.awdel;
            g.allowedit = p.awedit;
            g.allowmanage = p.awmag;
            g.allowpost = p.awpost;
            g.allowreply = p.awreply;
            g.color = p.color;
            ddc.SubmitChanges();
        }
    }

    public static void NewGroup(Permission p)//创建一个用户组
    {
        databaseDataContext ddc = new databaseDataContext();
        groups g = new groups();
        g.name = p.name;
        g.allowaccess = p.awaccess;
        g.allowdelete = p.awdel;
        g.allowedit = p.awedit;
        g.allowmanage = p.awmag;
        g.allowpost = p.awpost;
        g.allowreply = p.awreply;
        g.color = p.color;
        ddc.groups.InsertOnSubmit(g);
        ddc.SubmitChanges();
    }

    public static void NewNotice(CNotice cn)//保存新消息
    {
        databaseDataContext ddc = new databaseDataContext();
        notices n = new notices();
        n.fromUid = cn.fromUid;
        n.toUid = cn.toUid;
        n.message = cn.messageSend;
        n.tid = cn.tid;
        n.pid = cn.pid;
        n.sendtime = cn.sendtime;
        n.isReaded = cn.isReaded;
        n.address = cn.address;
        ddc.notices.InsertOnSubmit(n);
        ddc.SubmitChanges();
    }

    public static void UpdateNotice(CNotice cn)//更新消息
    {
        databaseDataContext ddc = new databaseDataContext();
        var query = from o in ddc.notices
                    where (o.nid == cn.nid)
                    select o;
        if(query.Count()!=0)
        {
            notices n = (query.ToList())[0];
            n.isReaded = 1;
            ddc.SubmitChanges();
        }
    }
}